import os
import re

from base_splitter import BaseSplitter


class Splitter1(BaseSplitter):
    """
    for book 《0001钢筋混凝土和砌体结构的抗震设计》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        self.chapter_pattern = re.compile(r"^## ?(?:\d+ .+?|[附 录]{2,})$", re.M)
        self.section_pattern = re.compile(r"^## ?\d ?\. ?\d+ .+?$", re.M)
        self.sub_sec_pattern = re.compile(r"^## ?(?:\d+\. ?){2}\d+ .+?$", re.M)


class Splitter2(BaseSplitter):
    """
    for book 《0002工程结构抗震》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        self.chapter_pattern = re.compile(r"^## ?第[一二三四五六七八九十 ]+?章 *.+?$", re.M)
        self.section_pattern = re.compile(r"^## ?[§\d\- {}]{3,}.+?$", re.M)
        self.sub_sec_pattern = re.compile(r"^## ?[一二三四五六七八九十、]{2,}.+?$", re.M)
        self.level_4_pattern = re.compile(r"^## ?[\d\. ]{2,}.+?$", re.M)


class Splitter3(BaseSplitter):
    """
    for book 《0003工程结构抗震分析》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        self.chapter_pattern = re.compile(r"^## ?第[0-9 ]+?章 *.+?$", re.M)
        self.section_pattern = re.compile(r"^## [§$\d\. {}]{4,}.+?$", re.M)
        self.sub_sec_pattern = re.compile(r"^## ?[\d\. ]{5,}.+?$", re.M)
        self.level_4_pattern = re.compile(r"^## ?[\d\. ]{7,}.+?$", re.M)


class Splitter4(BaseSplitter):
    """
    for book 《0004工程结构抗震设计.第3版》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        self.chapter_pattern = re.compile(r"^## ?\d+\s+.+?$", re.M)
        self.section_pattern = re.compile(r"^## ?[\d\. ]{3,}.+?$", re.M)
        self.sub_sec_pattern = re.compile(r"^## ?[\d\. ]{5,}.+?$", re.M)
        self.level_4_pattern = re.compile(r"^## ?\d\..+?$", re.M)


class Splitter5(Splitter2):
    """
    for book 《0005工程结构抗震与防灾.第2版》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        super().define_regex()
        self.chapter_pattern = re.compile(r"^## ?第[0-9 ]+?章 *.+?$", re.M)
        self.section_pattern = re.compile(r"^## ?[§$\d\-{} ]{3,}.+?$", re.M)
        self.level_4_pattern = re.compile(r"^## ?.\([一二三四五六七八九十 ]+\).+?$", re.M)


class Splitter6(Splitter3):
    """
    for book 《0006建筑结构抗震设计.第4版》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        super().define_regex()
        self.level_4_pattern = re.compile(r"^## ?\d\..+?$", re.M)


class Splitter7(Splitter4):
    """
    for book 《0007建筑结构抗震设计理论与实例-第2版》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        super().define_regex()
        self.chapter_pattern = re.compile(r"^## ?第[一二三四五六七八九十 0-9]+?章 *.+?$", re.M)


class Splitter8(Splitter3):
    """
    for book 《0008建筑抗震弹塑性分析.第2版》
    """

    def __init__(self, book_path) -> None:
        super().__init__(book_path)

    def define_regex(self):
        super().define_regex()
        self.chapter_pattern = re.compile(r"^## ?\d\s+.+?$", re.M)
        self.section_pattern = re.compile(r"^## ?[\d\. ]{3,}.+?$", re.M)


def test():
    books = r"""
raw_data\books\0007建筑结构抗震设计理论与实例-第2版.md
"""

    finished = []
    for name in books.strip().split("\n"):
        num = int(name.split("\\")[-1].split(".")[0][:4])
        if num in finished:
            continue
        book_name = os.path.splitext(os.path.basename(name))[0]
        book_path = name
        # load splitter dynamically by book index
        splitter: BaseSplitter = eval(f"Splitter{num}")(book_path)
        splitter.process_book(r"text_blocks\books")


if __name__ == "__main__":
    test()
